package cn.iocoder.yudao.module.system.service.messageInfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.messageInfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.messageInfo.MessageInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.system.convert.messageInfo.MessageInfoConvert;
import cn.iocoder.yudao.module.system.dal.mysql.messageInfo.MessageInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 站内信 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class MessageInfoServiceImpl implements MessageInfoService {

    @Resource
    private MessageInfoMapper messageInfoMapper;

    @Override
    public Long createMessageInfo(MessageInfoCreateReqVO createReqVO) {
        // 插入
        MessageInfoDO messageInfo = MessageInfoConvert.INSTANCE.convert(createReqVO);
        messageInfoMapper.insert(messageInfo);
        // 返回
        return messageInfo.getId();
    }

    @Override
    public void updateMessageInfo(MessageInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMessageInfoExists(updateReqVO.getId());
        // 更新
        MessageInfoDO updateObj = MessageInfoConvert.INSTANCE.convert(updateReqVO);
        messageInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMessageInfo(Long id) {
        // 校验存在
        this.validateMessageInfoExists(id);
        // 删除
        messageInfoMapper.deleteById(id);
    }

    private void validateMessageInfoExists(Long id) {
        if (messageInfoMapper.selectById(id) == null) {
            throw exception(MESSAGE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MessageInfoDO getMessageInfo(Long id) {
        return messageInfoMapper.selectById(id);
    }

    @Override
    public List<MessageInfoDO> getMessageInfoList(Collection<Long> ids) {
        return messageInfoMapper.selectBatchIds(ids);
    }

    @Override
    public List<MessageInfoDO> getMessageInfoList(Long messageId) {
        return messageInfoMapper.selectList(messageId);
    }

    @Override
    public PageResult<MessageInfoDO> getMessageInfoPage(MessageInfoPageReqVO pageReqVO) {
        return messageInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MessageInfoDO> getMessageInfoList(MessageInfoExportReqVO exportReqVO) {
        return messageInfoMapper.selectList(exportReqVO);
    }

}
